<?php
class JobLocationsController extends AppController {

	var $name = 'JobLocations';
	
	function beforeFilter(){
		if($this->Session->read('Auth.User')){
			$this->Auth->allow('*');
		}
	}
	
	function treeUpdate(){
		$this->params['url'] = Array('url' => 'admin/job_locations');
		if(($this->RequestHandler->isAjax())) {
			$id = isset($this->params['form']['name'])? $this->params['form']['name']: $this->params['named']['parentId'];
			$this->paginate = array(
								'conditions' => array('JobLocation.parent_id' => $id),
								'limit' => 20);
			$locations = $this->paginate();
			$this->set('locations', $locations);
			$this->set('id',$id);
		}
	}
	
	function locTree(){
		if(($this->RequestHandler->isAjax())) {
			$this->autoRender = false;
			$companies = $this->JobLocation->children();
			$this->set('treelist', $companies);
			$this->render('/elements/locTree');
		}
	}

	function admin_add($id = NULL,$edit = NULL) {
		if(($this->RequestHandler->isAjax())) {
			if($edit == NULL){
				$this->set('parent_id',$id);
			}else{
				$this->data = $this->JobLocation->read(null, $id);
				$parentJobLocations = $this->JobLocation->ParentJobLocation->find('list');
				$this->set(compact('parentJobLocations'));
			}
		}
	}
	
	function admin_save(){
		$this->autoRender = false;
		if (!empty($this->data)) {
			$date = time();
			$this->data['JobLocation']['user_id'] = 1;
			$this->data['JobLocation']['updated_date'] = strftime("%y-%m-%d",$date);
			$this->JobLocation->create();
			if ($this->JobLocation->save($this->data)) {
				$locations = $this->JobLocation->find('first', array(
														'conditions' => array('JobLocation.id' => $this->JobLocation->id)));
				$this->set('location',$locations);
				$this->Render('row');
			}
		}
	}
	
	function admin_index(){
		$sString = '';
		$this->set('top', false);
		$this->paginate = array(
							'contain' => false,
							'limit' => 20);
		if($this->RequestHandler->isAjax()) {
			if(isset($this->params['form']['name'])) $sString = $this->params['form']['name'];
			elseif(isset($this->params['named']['sString'])) $sString = $this->params['named']['sString'];
			
			if(!isset($this->params['named']['page']) && !isset($this->params['named']['sString']) && !isset($this->params['form']['name'])) {
				$conditions = array('JobLocation.parent_id' => NULL);
			} else {
				$conditions = array('JobLocation.name LIKE' => '%' . $sString . '%');
			}
			$this->set('sString', $sString);
			if(isset($this->params['named']['page']) || isset($this->params['form']['name']))
				$this->set('paginateView', true);
		}else{
			$this->set('top' , true);
			$conditions = array('JobLocation.parent_id' => NULL);
		}
		$this->set('treelist', $this->JobLocation->children());
		$this->set('locations', $this->paginate($conditions));
	}
	
	function admin_delete($id = null) {
		if(($this->RequestHandler->isAjax())) {
			$this->autoRender = false;
			if (!$id) {
				return 'error';
			}
			if ($this->JobLocation->delete($id)) {
				return 'Success';
			}
			return 'error';
		}
	}
	
	function get_location($id = NULL, $count = NULL){
		$this->autoRender = false;
		$this->set('count',1);
		if($count != NULL){
			$this->set('count',$count);
		}
		$locations = $this->JobLocation->children($id,true);
		$this->set('locations',$locations);
		$this->render('child');
	}
	
	function getAll(){
		return($this->JobLocation->find('list'));
	}
}
?>